package nccloud.web.platform.attachment.action;

import java.net.URL;
import java.util.Date;
import nc.bs.pub.filesystem.IFileSystemService;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.BusinessException;
import nc.vo.pub.filesystem.FileTypeConst;
import nc.vo.pub.filesystem.NCFileVO;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.core.json.IJson;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.action.itf.ICommonAction;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.container.SessionContext;
import nccloud.framework.web.json.JsonFactory;
import nccloud.pubitf.platform.attachment.IAttachmentService;
import nccloud.pubitf.platform.attachment.IFileStorageConst;
import nccloud.pubitf.platform.attachment.WebFileParaVO;
import nccloud.web.platform.attachment.tool.AttachmentButtonLimitUtils;


public class AttachmentUpLoadURLAction
  implements ICommonAction
{
  IAttachmentService ncservice = (IAttachmentService)ServiceLocator.find(IAttachmentService.class);

  
  public Object doAction(IRequest request) {
    String str = request.read();
    IJson json = JsonFactory.create();
    WebFileParaVO para = (WebFileParaVO)json.fromJson(str, WebFileParaVO.class);
    
    AttachmentButtonLimitUtils.getButtonLimit(para, "UPLOAD");
    
    validateParam(para);
    String billId = para.getBillId();
    String fullPath = para.getFullPath();
    String url = para.getUploadUrl();
    String filename = para.getName();
    
    filename = filename.replaceAll("http://", "");
    filename = filename.replaceAll("https://", "");
    
    NCFileVO newfilevo = new NCFileVO();
    String creator = SessionContext.getInstance().getClientInfo().getUserid();
    newfilevo.setCreator(creator);
    newfilevo.setPath(fullPath + "/" + filename);
    newfilevo.setFolder(false);
    newfilevo.setFiletype(FileTypeConst.URL);
    newfilevo.setTime((new Date()).getTime());
    newfilevo.setPk_doc(url);
    
    IFileSystemService fileservice = (IFileSystemService)ServiceLocator.find(IFileSystemService.class);
    try {
      boolean exist = fileservice.isExist(fullPath + "/" + filename);
      
      if (exist)
      {
        ExceptionUtils.wrapBusinessException(NCLangRes4VoTransl.getNCLangRes()
            .getStrByID("1501002_0", "01501002-0417", null, new String[] { filename }));
      }
      
      fileservice.insertURLFileVO(newfilevo);
    }
    catch (BusinessException e1) {
      
      ExceptionUtils.wrapBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("1501002_0", "01501002-0287"));
    } 
    
    NCFileVO[] ncfiles = this.ncservice.queryNCFilesByFullPaths(new String[] { fullPath + "/" + filename });

    
    WebFileParaConvertor convertor = new WebFileParaConvertor();
    
    return convertor.convertAfterUpload(ncfiles, billId, IFileStorageConst.Bucket);
  }


  
  private void validateParam(WebFileParaVO para) {
    String fullPath = para.getFullPath();
    if (fullPath == null || "".equals(fullPath))
    {
      ExceptionUtils.wrapBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("1501002_0", "01501002-0286"));
    }
    
    String url = para.getUploadUrl();
    if (url == null || "".equals(url))
    {
      ExceptionUtils.wrapBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("1501002_0", "01501002-0288"));
    }
    
    String filename = para.getName();
    if (filename == null || ".url".equals(filename.trim()))
    {
      ExceptionUtils.wrapBusinessException(NCLangRes4VoTransl.getNCLangRes()
          .getStrByID("1501002_0", "01501002-0289"));
    }
    
    try {
      if (url != null) {
        URL uRL = new URL(url);
      }
    }
    catch (Exception ex) {
      ExceptionUtils.wrapBusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("1501003_0", "01501003-0115"));
    } 
  }
}